/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.entity.ShopTag;
import com.thinkgem.jeesite.modules.shop.service.ShopTagService;

/**
 * 标签管理Controller
 * @author xxs
 * @version 2016-03-04
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/shopTag")
public class ShopTagController extends BaseController {

	@Autowired
	private ShopTagService shopTagService;
	
	@ModelAttribute
	public ShopTag get(@RequestParam(required=false) String id) {
		ShopTag entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = shopTagService.get(id);
		}
		if (entity == null){
			entity = new ShopTag();
		}
		return entity;
	}
	
	@RequiresPermissions("shop:shopTag:view")
	@RequestMapping(value = {"list", ""})
	public String list(ShopTag shopTag, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ShopTag> page = shopTagService.findPage(new Page<ShopTag>(request, response), shopTag); 
		model.addAttribute("page", page);
		return "modules/shop/shopTagList";
	}

	@RequiresPermissions("shop:shopTag:view")
	@RequestMapping(value = "form")
	public String form(ShopTag shopTag, Model model) {
		model.addAttribute("shopTag", shopTag);
		return "modules/shop/shopTagForm";
	}

	@RequiresPermissions("shop:shopTag:edit")
	@RequestMapping(value = "save")
	public String save(ShopTag shopTag, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, shopTag)){
			return form(shopTag, model);
		}
		shopTagService.save(shopTag);
		addMessage(redirectAttributes, "保存标签成功");
		return "redirect:"+Global.getAdminPath()+"/shop/shopTag/?repage";
	}
	
	@RequiresPermissions("shop:shopTag:edit")
	@RequestMapping(value = "delete")
	public String delete(ShopTag shopTag, RedirectAttributes redirectAttributes) {
		shopTagService.delete(shopTag);
		addMessage(redirectAttributes, "删除标签成功");
		return "redirect:"+Global.getAdminPath()+"/shop/shopTag/?repage";
	}

}